package com.hwy.catering.config;

import com.hwy.catering.interceptor.*;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.util.List;

@Configuration
public class MvcConfig implements WebMvcConfigurer {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    //添加拦截器，让写的那个LoginInterceptor拦截器生效
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //登录拦截器，order越高，越后执行
        registry.addInterceptor(new LoginInterceptor())
                //LoginInterceptor拦截器哪些路径不拦截
                .excludePathPatterns(
                        "/cater/user/login",
                        "/cater/user/logout",
                        "/cater/user/test",
                        //swagger要配置4个路径不被拦截
                        "/swagger-resources/**",
                        "/webjars/**",
                        "/v2/**",
                        "/swagger-ui.html/**",
                        //导出文件前端的window.open方法添加不了请求头
                        "/cater/user/exportUser",
                        "/cater/dish/exportDish"
                ).order(2);
        //token刷新拦截器
        registry.addInterceptor(new RefreshTokenInterceptor(stringRedisTemplate)).addPathPatterns("/**").order(1);
        registry.addInterceptor(new RootInterceptor(stringRedisTemplate)).addPathPatterns("/**").order(3);
        registry.addInterceptor(new StopInterceptor(stringRedisTemplate))
                .excludePathPatterns(
                        "/cater/user/login",
                        "/cater/user/logout",
                        "/cater/user/test",
                        //swagger要配置4个路径不被拦截
                        "/swagger-resources/**",
                        "/webjars/**",
                        "/v2/**",
                        "/swagger-ui.html/**",
                        //导出文件前端的window.open方法添加不了请求头
                        "/cater/user/exportUser",
                        "/cater/dish/exportDish"
                ).order(4);
        registry.addInterceptor(new AccessLimitInterceptor(stringRedisTemplate)).addPathPatterns("/**").order(0);
    }



}
